<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;

use think\Validate;
use think\Db;

class AdminHelpController extends AdminBaseController
{

  public function index() {
    $list = Db::name('help')->orderRaw('id desc')->paginate(10);
    $this->assign('list', $list);
    return $this->fetch();
  }

  public function addForm() {
    $param = $this->request->param();
    Db::name('help')->insert([
      'title' => $param['title'],
      'content' => $param['content'],
    ]);

    $this->success('添加成功');
  }

  public function edit() {
    $id = input('id', false);
    if (!$id) {
      $this->error('参数不合法！');
    }

    $data = Db::name('help')->where('id', $id)->find();
    $data['content'] = htmlspecialchars_decode($data['content']);
    $this->assign('data', $data);
    return $this->fetch();
  }

  public function editForm() {
    $validate = new Validate([
      'title' => 'require',
      'content' => 'require',
    ]);

    $validate->message([
      'title.require' => '标题不能为空！',
      'content.require' => '详情内容不能为空！',
    ]);

    $param = $this->request->param();
    if (!$validate->check($param)) {
      $this->error($validate->getError());
    }

    Db::name('help')->where('id', $param['id'])->update([
      'title' => $param['title'],
      'content' => $param['content'],
    ]);

    $this->success('修改成功');
  }

  public function delete() {
    $id = input('id', false);
    if ($id === false) {
      $this->error('参数不合法！');
    }
    Db::name('help')->delete($id);
    $this->success('删除成功');
  }

}
